<template>
  <div class="container">
    <div style="background-color: white;width: 400px;padding: 30px;border-radius: 10px">
      <div style="text-align: center;font-size: 20px;margin-bottom: 20px;color: #000000 ">欢迎使用非遗智创社区</div>
      <el-form ref="formRef" :model="form" :rules="rules">
        <el-form-item prop="username">
          <el-input v-model="form.username" placeholder="请输入账号" prefix-icon="el-icon-user"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="form.password" placeholder="请输入密码" prefix-icon="el-icon-lock"></el-input>
        </el-form-item>
        <el-form-item prop="confirmPwd">
          <el-input v-model="form.confirmPwd" placeholder="请确认密码" prefix-icon="el-icon-lock"></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <div style="display: flex">
            <el-input v-model="form.code" placeholder="验证码" prefix-icon="el-icon-postcard"
                      @keyup.enter.native="register"></el-input>
            <div class="login-code">
              <img id="verificationCodeImg" :src="codeUrl" alt="点击一下试试" title="看不清？换一张" @click="captcha"/>
            </div>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button style="width: 100%;background: pink;border-color: #ff7b7b;color: white" @click="register">注 册
          </el-button>
        </el-form-item>
        <div style="display: flex;align-items: center">
          <div style="flex: 1"></div>
          <div style="flex: 1;text-align: right">
            已有账号？请<a href="/Login">登录</a>
          </div>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import {captcha, register} from "@/api/request/WebRequest";

export default {
  data() {
    const validatePassword = ((rule, confirmPwd, callback) => {
      if (confirmPwd === '') {
        callback(new Error("请确认密码"))
      } else if (confirmPwd !== this.form.password) {
        callback(new Error("两次输入的密码不一致"))
      } else {
        callback()
      }
    })
    return {
      form: {},
      codeUrl: "",
      rules: {
        username: [
          {required: true, message: '请输入账号', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'}
        ],
        confirmPwd: [
          {required: true, message: '请确认密码', trigger: 'blur'},
          {validator: validatePassword, trigger: 'blur'}
        ],
        code: [
          {required: true, message: "请输入验证码", trigger: "change"}
        ]
      }
    }
  },
  methods: {
    captcha() {
      captcha().then(imageUrl => {
        if (imageUrl) {
          this.codeUrl = imageUrl;
        }
      });
    },
    register() {
      this.$refs['formRef'].validate((valid) => {
        if (valid) {
          register(this.form).then(res => {
            if (res.code !== 200) {
              this.$message.error(res.msg)
              return
            }

            this.$router.push('/Login')
            this.$message.success('注册成功')
          }).catch(error => {
            console.log(error)
          }).finally(() => {
            this.captcha();
          })
        }
      })
    }
  },
  mounted() {
    this.captcha();
  }
}
</script>

<style scoped>
.container {
  background: url("@/assets/image/0101dd61-df94-42a3-89ae-d00fe9a08496.jpg") no-repeat center;
  background-size: cover;
  height: 100vh;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #858585;
}


.login-code {
  width: 150px;
  text-align: center;
  height: 41px;
  margin-left: 10px;
}

.login-code img {
  cursor: pointer;
}

.login-code-img {
  height: 100%;
}
</style>
